package com.lsp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class OpenAutoconfigPrincipeApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenAutoconfigPrincipeApplication.class, args);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "/testRedis")
    public String testRedis(){
        redisTemplate.opsForValue().set("test", "12346");
        return "OK";
    }

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template =  new RedisTemplate<>();
        //不用jdk默认的编码
        template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
